package com.prp.db.daoimpl.mysql;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.prp.bo.SNSConstants;
import com.prp.db.idao.IScoreRecordDAO;
import com.prp.db.po.ScoreRecord;
import com.prp.db.po.SearchRecord;

/**
 * A data access object (DAO) providing persistence and search support for
 * ScoreRecord entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.prp.db.daoimpl.mysql.ScoreRecord
 * @author MyEclipse Persistence Tools
 */

public class ScoreRecordDAO extends BaseHibernateDAO implements IScoreRecordDAO {
	private static final Log log = LogFactory.getLog(ScoreRecordDAO.class);
	// property constants
	public static final String SCORE = "score";

	public void save(ScoreRecord transientInstance) {
		log.debug("saving ScoreRecord instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ScoreRecord persistentInstance) {
		log.debug("deleting ScoreRecord instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ScoreRecord findById(java.lang.Integer id) {
		log.debug("getting ScoreRecord instance with id: " + id);
		try {
			ScoreRecord instance = (ScoreRecord) getSession().get(
					"com.prp.db.daoimpl.mysql.ScoreRecord", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ScoreRecord instance) {
		log.debug("finding ScoreRecord instance by example");
		try {
			List results = getSession().createCriteria(
					"com.prp.db.daoimpl.mysql.ScoreRecord").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ScoreRecord instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ScoreRecord as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByScore(Object score) {
		return findByProperty(SCORE, score);
	}

	public List findAll() {
		log.debug("finding all ScoreRecord instances");
		try {
			String queryString = "from ScoreRecord";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ScoreRecord merge(ScoreRecord detachedInstance) {
		log.debug("merging ScoreRecord instance");
		try {
			ScoreRecord result = (ScoreRecord) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ScoreRecord instance) {
		log.debug("attaching dirty ScoreRecord instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ScoreRecord instance) {
		log.debug("attaching clean ScoreRecord instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public Integer findByUserIdSubjectId(int userId, Integer toSubjectId) {
		// TODO Auto-generated method stub
		log.debug("finding ScoreRecord instance with property: "
				+ SNSConstants.ScoreRecord_UserId + ","
				+ SNSConstants.ScoreRecord_SubjectId + ", value: " + userId
				+ "," + toSubjectId);
		try {
			String queryString = "from ScoreRecord as model where model."
					+ SNSConstants.ScoreRecord_UserId + "= ? and model."
					+ SNSConstants.ScoreRecord_SubjectId + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, userId);
			queryObject.setParameter(1, toSubjectId);
			List list = queryObject.list();
			if (list == null || list.size() == 0)
				return null;
			return ((ScoreRecord) queryObject.list().get(0)).getScore();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
}